package com.dikang.keer.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dikang.common.utils.PageUtils;
import com.dikang.common.utils.Query;
import com.dikang.keer.product.entity.CategoryBrandRelationEntity;
import com.dikang.keer.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dikang.keer.product.dao.CategoryDao;
import com.dikang.keer.product.entity.CategoryEntity;
import com.dikang.keer.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
   @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWtihTree() {
        // 获取所有的分类
        List<CategoryEntity>   categoryList = baseMapper.selectList(null);
        // 组成父子的树形结构
        // 查找出一级目录
        List<CategoryEntity> categoryEntities = categoryList.stream()
                .filter((item) -> item.getParentCid() == 0)
                .map(menu -> {
                    menu.setChildren(this.getChildren(menu, categoryList));
                    // 递归
                    return menu;
                }).sorted((menu1, menu2) -> {
                    return (menu1.getSort() == null ? 0 : menu1.getSort()) -( menu2.getSort() == null ? 0 : menu2.getSort());
                }).collect(Collectors.toList());

        return categoryEntities;
    }
    private List<CategoryEntity> getChildren(CategoryEntity category,List<CategoryEntity> list){
        List<CategoryEntity> collect = list.stream()
                .filter(item -> category.getCatId().equals(item.getParentCid()))
                .map(children -> {
                    children.setChildren(getChildren(children, list));
                    return children;
                })
                .sorted((menu1, menu2) -> {
                    return (menu1.getSort() == null ? 0 : menu1.getSort()) -( menu2.getSort() == null ? 0 : menu2.getSort());
                }).collect(Collectors.toList());


        return collect;
    }
    @Override
    public Long[] getcatelogPath(Long catelogId) {
        // 查询出这个表当中的所有元素，然后逐个去找上一层
        ArrayList<Long> catelogs = new ArrayList<>();
        List<Long> parentPath = this.getParentPath(catelogId, catelogs);
        Collections.reverse(parentPath);

        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    public void updateCategory(CategoryEntity category) {
        baseMapper.updateById(category);
        // 修改连接表
        UpdateWrapper<CategoryBrandRelationEntity> wrapper = new UpdateWrapper<>();
        wrapper.set("cat_id",category.getCatId());
        wrapper.set("name",category.getName());
        categoryBrandRelationService.update(wrapper);


    }

    // 寻找父节点
    public List<Long> getParentPath(Long catelogId , List<Long> paths){
        paths.add(catelogId);
        Long id = this.getById(catelogId).getParentCid();
        if ( id !=0){
            this.getParentPath(id,paths);
        }
        return  paths;
    }
}